package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.Administrator;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class AdministratorController {

    @Autowired
    private AdministratorService administratorService;

    @PostMapping("administrator")
    public Result add(@RequestBody Administrator administrator){
        administratorService.add(administrator);
        return new Result(200,"添加成功",true);
    }

    @GetMapping("administrator/list")
    public Result list(){
        List<Administrator> list = administratorService.list();
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        return new Result(200,"所有管理员",true,map);
    }

    //获取单个用户信息
    @GetMapping("administrator/{id}")
    public Result get(@PathVariable("id") Integer id){

        if(null == id){
            return new Result(400,"id不能为空",false);
        }
        Administrator administrator = administratorService.getById(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("administrator",administrator);
        return new Result(200,"管理员用户信息",true,map);

    }

    @PutMapping("administrator")
    public Result update(@RequestBody Administrator administrator){
        administratorService.add(administrator);
        return new Result(200,"修改用户信息成功",true);

    }

    @DeleteMapping("administrator/{id}")
    public Result delete(@PathVariable("id") Integer id){
        administratorService.delete(id);
        return new Result(200,"删除用户信息成功",true);
    }

}
